package com.ruoyi.web.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.UtilTool;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.mail.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.util.Date;
import java.util.List;

/**
 * @author jiebin
 * @date 2021/7/13 15:55
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class AppUserAPiController extends BaseController {

    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IAppUserFeedbackService feedbackService;
    @Autowired
    private IAppVersionService versionService;
    @Autowired
    private IAppStartPageService startPageService;
    @Autowired
    private MailService mailService;
    @Autowired
    private IAppUserMsgService appUserMsgService;

    /**
     * 添加用户
     * @param appUser
     * @return
     */
    @PostMapping("/addAppUser")
    @ResponseBody
    public AjaxResult addAppUser(AppUser appUser) {
        AppUser app = appUserService.getAppUserByUserName(appUser.getUserName());
        if (app==null){
            if (!UtilTool.isNull(appUser.getUuid())){
                AppUser user = appUserService.getAppUserByUuid(appUser.getUuid());
                if (!UtilTool.isNull(user)){
                    user.setType(0);
                    user.setUserName(appUser.getUserName());
                    user.setPassword(appUser.getPassword());
                    user.setNickName(appUser.getNickName());
                    user.setEmail(appUser.getEmail());
                    user.setPhone(appUser.getPhone());
                    user.setHeadUrl(appUser.getHeadUrl());
                    user.setCreateTime(new Date(System.currentTimeMillis()));
                    user.setUpdateTime(new Date(System.currentTimeMillis()));
                    return toAjax(appUserService.updateAppUser(user));
                }else {
                    appUser.setType(0);
                    appUser.setUseNum(0);
                    appUser.setCreateTime(new Date(System.currentTimeMillis()));
                    appUser.setUpdateTime(new Date(System.currentTimeMillis()));
                    return toAjax(appUserService.insertAppUser(appUser));
                }
            }else{
                return AjaxResult.error("手机识别码不能为空");
            }
        }else {
            return AjaxResult.error("该账号已注册");
        }
    }

    /**
     * 更新用户
     * @param appUser
     * @return
     */
    @PostMapping("/updateAppUser")
    @ResponseBody
    public AjaxResult updateAppUser(AppUser appUser) {
        appUser.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(appUserService.updateAppUser(appUser));
    }

    /**
     * 更新设备uuid
     * @param appUser
     * @return
     */
    @PostMapping("/updateUuid")
    @ResponseBody
    public AjaxResult updateUuid(AppUser appUser) {
        log.info("参数值为："+ JSONObject.toJSONString(appUser));
        List<AppUser> apps = appUserService.selectAppUserList(appUser);
        if (apps.size()==0){
            if (UtilTool.isNull(appUser.getUuid())){
                return AjaxResult.error("uuid不能为空");
            }
            appUser.setType(1);
            appUser.setCreateTime(new Date(System.currentTimeMillis()));
            appUser.setUpdateTime(new Date(System.currentTimeMillis()));
            appUser.setUseNum(1);
            return toAjax(appUserService.insertAppUser(appUser));
        }else {
            AppUser app = apps.get(0);
            app.setUpdateTime(new Date(System.currentTimeMillis()));
            app.setUseNum(app.getUseNum()+1);
            log.info("------------准备+1："+ JSONObject.toJSONString(app));
            return toAjax(appUserService.updateAppUser(app));
        }
    }

    /**
     * 登陆
     * @param appUser
     * @return 登陆账号用账号做标识
     */
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(AppUser appUser){
        if (UtilTool.isNull(appUser.getUserName()) || UtilTool.isNull(appUser.getPassword())){
            return AjaxResult.error("请输入账号或者密码");
        }else {
            AppUser app = appUserService.getAppUserByUserNameAndPassword(appUser);
            if (UtilTool.isNull(app)){
                return AjaxResult.error("密码错误，请重试");
            }else {
                app.setUpdateTime(new Date(System.currentTimeMillis()));
                appUserService.updateAppUser(app);
                return AjaxResult.success(app);
            }
        }
    }

    /**
     * 添加反馈内容
     * @param feedback
     * @return
     */
    @PostMapping("/addFeedback")
    @ResponseBody
    public AjaxResult addFeedback(AppUserFeedback feedback){
        if (UtilTool.isNull(feedback.getAppUserName())){
            feedback.setAppNickName(feedback.getPhoneUuid());
            feedback.setCreateTime(new Date(System.currentTimeMillis()));
            feedback.setUpdateTime(new Date(System.currentTimeMillis()));
            return toAjax(feedbackService.insertAppUserFeedback(feedback));
        }else {
            AppUser appUser = appUserService.getAppUserByUserName(feedback.getAppUserName());
            if (UtilTool.isNull(appUser)){
                return AjaxResult.error("用户登陆失效，请登陆后再添加反馈内容");
            }
            feedback.setAppUserId(appUser.getId());
            feedback.setAppNickName(appUser.getNickName());
            feedback.setCreateTime(new Date(System.currentTimeMillis()));
            feedback.setUpdateTime(new Date(System.currentTimeMillis()));
            return toAjax(feedbackService.insertAppUserFeedback(feedback));
        }
    }

    /**
     * 获取最新版
     * @return
     */
    @PostMapping("/getAppVersion")
    @ResponseBody
    public AjaxResult getAppVersion(){
        AppVersion version = versionService.getNewestAppVersion();
        if (UtilTool.isNull(version)){
            version = new AppVersion();
            version.setAppVersion("1.0");
            return AjaxResult.success(version);
        }else {
            return AjaxResult.success(version);
        }
    }

    /**
     * 通过版本号获取版本
     * @return
     */
    @PostMapping("/getAppVersionByAppVersion")
    @ResponseBody
    public AjaxResult getAppVersionByAppVersion(AppVersion version){
        List<AppVersion> versions = versionService.selectAppVersionList(version);
        if (versions.size()==0){
            return AjaxResult.error("无此版本号");
        }else {
            return AjaxResult.success(versions.get(versions.size()-1));
        }
    }

    /**
     * 获取app启动页内容
     * @return
     */
    @PostMapping("/getAppStartPage")
    @ResponseBody
    public AjaxResult getAppStartPage(){
        AppStartPage appStartPage = startPageService.getAppStartPage();
        if (UtilTool.isNull(appStartPage)){
            return AjaxResult.error("没有设置内容");
        }else {
            return AjaxResult.success(appStartPage);
        }
    }

    /**
     * 邮箱重置密码
     * @return
     */
    @PostMapping("/resetPwByEmail")
    @ResponseBody
    public AjaxResult resetPwByEmail(AppUser appUser){
        if (UtilTool.isNull(appUser.getUserName())){
            return AjaxResult.error("请输入账号");
        }
        String email = appUser.getEmail();
        appUser = appUserService.getAppUserByUserName(appUser.getUserName());
        if (UtilTool.isNull(appUser)){
            return AjaxResult.error("该账号没有注册");
        }else {
            String content = "<html>\n" +
                    "<body>\n" +
                    "<h3>您好，您的账号密码已经重置为123456。如果本封邮件非您本人触发，请忽略。</h3>\n" +
                    "<body>\n" +
                    "</html>\n";
            try {
                mailService.sendHtmlMail(email,"闪火重置密码通知",content);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            appUser.setPassword("123456");
            appUser.setUpdateTime(new Date(System.currentTimeMillis()));
            appUserService.updateAppUser(appUser);
            return AjaxResult.success();
        }
    }

    /**
     * 添加用户同步数据
     * @return
     */
    @PostMapping("/addAppUserMsg")
    @ResponseBody
    public AjaxResult addAppUserMsg(AppUserMsg appUserMsg){
        if (UtilTool.isNull(appUserMsg.getUserName()) || UtilTool.isNull(appUserMsg.getContent())){
            return AjaxResult.error("信息不能为空");
        }
        AppUserMsg msg = new AppUserMsg();
        msg.setUserName(appUserMsg.getUserName());
        List<AppUserMsg> msgList = appUserMsgService.selectAppUserMsgList(msg);
        if (msgList.size()==0){
            return toAjax(appUserMsgService.insertAppUserMsg(appUserMsg));
        }else {
            msg = msgList.get(0);
            msg.setContent(appUserMsg.getContent());
            return toAjax(appUserMsgService.updateAppUserMsg(msg));
        }
    }

    /**
     * 获取用户同步数据
     * @return
     */
    @PostMapping("/getAppUserMsg")
    @ResponseBody
    public AjaxResult getAppUserMsg(AppUserMsg appUserMsg){
        if (UtilTool.isNull(appUserMsg.getUserName())){
            return AjaxResult.error("信息不能为空");
        }
        List<AppUserMsg> msgList = appUserMsgService.selectAppUserMsgList(appUserMsg);
        if (msgList.size()==0){
            return AjaxResult.error("该账号未同步过");
        }else {
            appUserMsg = msgList.get(0);
            return AjaxResult.success(appUserMsg);
        }
    }


    @PostMapping("/demo")
    @ResponseBody
    public AjaxResult demo() throws MessagingException {
        String content = "<html>\n" +
                "<body>\n" +
                "<h3>您好，您的账号密码已经重置为123456。如果本封邮件非您本人触发，请忽略。</h3>\n" +
                "<body>\n" +
                "</html>\n";
        mailService.sendHtmlMail("13609084565@163.com","闪火重置密码通知",content);
        return AjaxResult.success();
    }


}
